<?php

/*
 * What php team is that is 'one thing, a team, work together'
 */

use App\Exceptions\CustomException;
use App\Service\CommonService;

if (!function_exists('logParams')) {
    function logParams($message, array $array = null)
    {
        if ($array) {
            \Log::channel('params_daily')->info($message, $array);
        } else {
            \Log::channel('params_daily')->info($message);
        }
    }
}

if (!function_exists('logInfo')) {
    function logInfo($message, array $array = null)
    {
        if ($array) {
            \Log::channel('info_daily')->info($message, $array);
        } else {
            \Log::channel('info_daily')->info($message);
        }
    }
}

if (!function_exists('logRequest')) {
    function logRequest($message, array $array = null)
    {
        if ($array) {
            \Log::channel('request_daily')->info($message, $array);
        } else {
            \Log::channel('request_daily')->info($message);
        }
    }
}

if (!function_exists('logQuery')) {
    function logQuery($message, array $array = null)
    {
        if ($array) {
            \Log::channel('query_daily')->info($message, $array);
        } else {
            \Log::channel('query_daily')->info($message);
        }
    }
}

if (!function_exists('logError')) {
    function logError($message, array $array = null)
    {
        if ($array) {
            \Log::channel('error_daily')->error($message, $array);
        } else {
            \Log::channel('error_daily')->error($message);
        }
    }
}

if (!function_exists('getDir')) {
    function getDir($path)
    {
        if (is_dir($path)) {
            return true;
        }
        $res = mkdir(iconv('UTF-8', 'GBK', $path), 0777, true);
        if ($res) {
            return true;
        }

        return false;
    }
}

if (!function_exists('customError')) {
    function customError($message, $code = 500)
    {
        throw new CustomException($message, intval($code));
    }
}

if (!function_exists('currentDatetime')) {
    function currentDatetime($datetime = null)
    {
        return $datetime ? $datetime->toDateTimeString() : now()->toDateTimeString();
    }
}

if (!function_exists('handleOne')) {
    function handleOne($collection, $column)
    {
        if (!$collection) {
            return '';
        }
        if (is_array($column)) {
            return $collection->only($column);
        }

        return $collection->{$column};
    }
}

if (!function_exists('handleMany')) {
    function handleMany($collection, $column)
    {
        if (!$collection->count()) {
            return [];
        }

        return $collection->map(function ($item) use ($column) {
            return $item->only($column);
        })->toArray();
    }
}

if (!function_exists('getFileContent')) {
    function getFileContent($filename)
    {
        $content = file_get_contents($filename);
        if (false === $content) {
            customError('获取文件内容失败');
        }

        return $content;
    }
}

if (!function_exists('putFileContent')) {
    function putFileContent($filename, $content)
    {
        if (false === file_put_contents($filename, $content)) {
            customError('写入文件内容失败');
        }
    }
}

if (!function_exists('responseData')) {
    function responseData($data = array(), $code = 200, $msg = '')
    {
        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : 'http://annual.gengyong.com.cn';
        header('Content-Type:application/json; charset=utf-8');
        header("Access-Control-Allow-Origin:".$origin);
        header("Access-Control-Allow-Credentials: true ");
        header("request_id: ".REQUESTID);
        header('Access-Control-Allow-Headers:*');
        header('Access-Control-Allow-Methods:*');
        header('Access-Control-Expose-Headers: *');
        $response = ['data'=>$data,'code'=>$code,'msg'=>$msg];
        CommonService::respLog($response);
        return json_encode($response);
    }
}

if (!function_exists('responseTextData')) {
    function responseTextData($data = array(), $code = 0, $msg = '')
    {
        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : 'http://annual.gengyong.com.cn';
        header('Content-Type:application/json; charset=utf-8');
        header("Access-Control-Allow-Origin:". $origin);
        header("Access-Control-Allow-Credentials: true ");
        header("request_id: ".REQUESTID);
        $response = ['data'=>$data,'errno'=>$code,'msg'=>$msg];
        CommonService::respLog($response);
        return json_encode($response);
    }
}


if (!function_exists('buildOrderNo')) {
    /**生成唯一订单号
     *@return int 返回16位的唯一订单号
     */
    function buildOrderNo(){
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $num = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));

        // return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        return $num;
    }
}

if(!function_exists('createInviteCode')){
    // 大代理、业务员、会员注册成功生成邀请码-6位
    function createInviteCode(){
        $code = substr(base_convert(md5(uniqid(md5(microtime(true)),true)), 16, 10), 0, 6);
        return $code;
    }
}

if(!function_exists('objectToArray')){
    /**
     * 对象转数组
     */
    function objectToArray($object) {
        //先编码成json字符串，再解码成数组
        return json_decode(json_encode($object), true);
    }
}
